let app = getApp();
import { util } from '../../../utils/util';
Page({
  data:{
    sex:'男',   //性别
    nation : '汉',  
    array: ['请选择','群众', '团员', '预备党员', '党员'],
    nationList:["请选择","汉族","蒙古族","回族","藏族","维吾尔族","苗族","彝族","壮族","布依族","朝鲜族","满族","侗族","瑶族","白族","土家族","哈尼族","哈萨克族","傣族","黎族","僳僳族","佤族","畲族","高山族","拉祜族","水族","东乡族","纳西族","景颇族","柯尔克孜族","土族","达斡尔族","仫佬族","羌族","布朗族","撒拉族","毛南族","仡佬族","锡伯族","阿昌族","普米族","塔吉克族","怒族","乌孜别克族","俄罗斯族","鄂温克族","德昂族","保安族","裕固族","京族","塔塔尔族","独龙族","鄂伦春族","赫哲族","门巴族","珞巴族","基诺族"],
    index: 0, 
    nationListIndex:0,
    nativeplace : '',  //籍贯
    studentcode : '', //学籍号
    IDCard : '' //身份证号
  },
  onClicknation: function(e){ //民族选择器
    console.log('more发送选择改变，携带值为', e.detail.value);
    if(e.detail.value==0){util.showError("请选择民族");return}
    this.setData({
      nationListIndex: e.detail.value,
    });
    var data = {
      national : this.data.nationList[this.data.nationListIndex]
    }
    util.updateUser(data).then(function(res){ 
      console.log('获取名族成功----',res)
      if(res.data.code==0){
          wx.showToast({
            icon: 'success',
            title: '修改成功',
            duration: 3000,
           });
     }else{
      util.showError(res.data.message);
     }
    });
  },
  bindPickerChange(e){ //点击政治面貌选择事件
    console.log('more发送选择改变，携带值为', e.detail.value);
    if(e.detail.value==0){util.showError("请选择政治面貌");return}
    this.setData({
      index: e.detail.value,
    });
    var data = {
      politicsStatus : this.data.array[this.data.index]
    }
    util.updateUser(data).then(function(res){ 
        if(res.data.code==0){
          wx.showToast({
          icon: 'success',
          title: '修改成功',
          duration: 3000,
          });
        }else{
        util.showError(res.data.message);
        }
    });
            
  },
  onLoad(query) {
    let _this = this
    // 页面加载
    console.info(`Page onLoad with query: ${JSON.stringify(query)}`);
    if(app.Role=='2'){
       this.setData({
          teache_Boolean:true,
          public_Boolean: false,
          
         })
      }else{
        this.setData({
          teache_Boolean:false,
          public_Boolean: true,
         })
      }

      util.getUserInfo().then(function(res){
        if(res.data.code==0){

            _this.setData({
              sex: res.data.info.user.sex==1?'男':'女',    //性别
              nation : res.data.info.user.national||'暂无',   //民族
              nativeplace : res.data.info.user.nativePlace||'暂无', //籍贯
              studentcode : res.data.info.currentSemester.id||'暂无', //学籍号
              IDCard : res.data.info.user.idCard||'暂无', //身份证号  
            })

            let nation=res.data.info.user.national;
            let nationList=_this.data.nationList;
            for(let i=0;i<nationList.length;i++){
              if(nation==nationList[i]){
                _this.setData({
                  nationListIndex:i
                })
              }
            }

            let politicsStatus=res.data.info.user.politicsStatus;
            let array=_this.data.array;
            for(let i=0;i<array.length;i++){
              if(politicsStatus==array[i]){
                _this.setData({
                  index:i
                })
              }
            }
            
          }else{
           util.showError(res.data.message);
          }
      });
  },
  onReady() {
    // 页面加载完成
  },
  onShow() {
    // 页面显示
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: 'My App',
      desc: 'My App description',
      path: 'pages/index/index',
    };
  },
});
